package com.service.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dao.SysMenuMapper;
import com.entity.SysMenu;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.SysMenuService;
import com.utils.BaseService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author lanouhn
 */
@Service
public class SysMenuServiceImpl extends BaseService implements SysMenuService{
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public long getSysMenuRowCount(){
        return sysMenuMapper.getSysMenuRowCount();
    }
    @Override
    public List<SysMenu> selectSysMenu(){
        return sysMenuMapper.selectSysMenu();
    }
    @Override
    public SysMenu selectSysMenuByObj(SysMenu obj){
        return sysMenuMapper.selectSysMenuByObj(obj);
    }
    @Override
    public SysMenu selectSysMenuById(Integer id){
        return sysMenuMapper.selectSysMenuById(id);
    }
    @Override
    public int insertSysMenu(SysMenu value){
        return sysMenuMapper.insertSysMenu(value);
    }
    @Override
    public int insertNonEmptySysMenu(SysMenu value){
        return sysMenuMapper.insertNonEmptySysMenu(value);
    }
    @Override
    public int insertSysMenuByBatch(List<SysMenu> value){
        return sysMenuMapper.insertSysMenuByBatch(value);
    }
    @Override
    public int deleteSysMenuById(Integer id){
        return sysMenuMapper.deleteSysMenuById(id);
    }
    @Override
    public int updateSysMenuById(SysMenu enti){
        return sysMenuMapper.updateSysMenuById(enti);
    }
    @Override
    public int updateNonEmptySysMenuById(SysMenu enti){
        return sysMenuMapper.updateNonEmptySysMenuById(enti);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(SysMenuMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<SysMenu> pageInfo = new PageInfo<SysMenu>(sysMenuMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<SysMenu>());

        return dataTables;
    }

    @Override
    public Map<String, Object> addSysMenu(SysMenu sysMenu) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysMenuMapper.insertSysMenu(sysMenu);
        System.out.println(id);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> updateSysMenu(SysMenu sysMenu) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysMenuMapper.updateNonEmptySysMenuById(sysMenu);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteByPrimaryKeys(List<Integer> idList) {
        Map<String, Object> map = new HashMap<>();
        Integer id = sysMenuMapper.deleteByPrimaryKeys(idList);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    public SysMenuMapper getSysMenuMapper() {
        return this.sysMenuMapper;
    }

    public void setSysMenuMapper(SysMenuMapper sysMenuMapper) {
        this.sysMenuMapper = sysMenuMapper;
    }

}